package controller.auth;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.auth.Authorities;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import service.auth.AuthoritiesService;
import core.base.controller.BaseSpringController;
import core.base.controller.PageHelper;
import core.base.page.Page;
import core.base.page.PageRequest;
import core.util.ParamUtil;
import core.util.StringUtil;

@Controller("admin.auth.AuthoritiesAdminController")
@RequestMapping("/admin/auth/auth")
public class AuthoritiesAdminController extends BaseSpringController {

	private String redirectView = "admin/common/redirect";
	private String queryView = "admin/auth/query";
	private String incListView = "admin/auth/inclist";
	private String addView = "admin/auth/resadd";
	private String viewView = "admin/auth/resview";
	
	@Autowired
	@Qualifier("auth.AuthoritiesService")
	private AuthoritiesService authoritiesService;

	@RequestMapping("query.htm")
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView(queryView);
	}
	

	@RequestMapping("inc_list.htm")
	public ModelAndView incList(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		PageRequest<Map> pageReq = new PageRequest<Map>();
		HashMap<String, Object> filter = new HashMap<String, Object>();
		
		PageHelper pageHelper = new PageHelper(request);
		// 获取请求的显示的页数,如为空,则取默认第1页
		int pageNum = ParamUtil.getInt(request, "pageNum", 1);
		// 设置默认每页显示记录数
		int pageSize = pageHelper.doCustomPerPage(DEFAULT_PAGESIZE);
		pageReq.setFilters(filter);
		pageReq.setPageNumber(pageNum);
		pageReq.setPageSize(pageSize);
		
		Page<Authorities> page = authoritiesService.queryForPage(pageReq);
		popPageInfo(page, modelMap);
		
		//如果需要定制可以设置paramName，默认使用queryString
		//page.setParamName("method=index&");
		String pageBreakStr = pageHelper.getPageBreakStr(page);
		modelMap.put("pageBreakStr", pageBreakStr);
		return new ModelAndView(incListView).addAllObjects(modelMap);
	}
	
	@RequestMapping("add.htm")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		return new ModelAndView(addView).addAllObjects(modelMap);
	}
	
	@RequestMapping("addsave.htm")
	public ModelAndView addSave(HttpServletRequest request, HttpServletResponse response) {
		Authorities model = new Authorities();
		model.setAuthCode(request.getParameter("authCode"));
		model.setAuthName(request.getParameter("authName"));
		model.setAuthDesc(request.getParameter("authDesc"));
		
		if ("1".equals(StringUtil.defaultStringIfBlank(request.getParameter("enabled"),"1"))) {
			model.setEnabled(1);
		}else{
			model.setEnabled(0);
		}
		if ("1".equals(StringUtil.defaultStringIfBlank(request.getParameter("issys"),"1"))) {
			model.setEnabled(1);
		}else{
			model.setEnabled(0);
		}
		// TODO check code是否已存在; 检查数据格式
		authoritiesService.insert(model);
		String redirecturl = request.getContextPath() + "/admin/auth/auth/query.htm";
		return new ModelAndView(redirectView).addObject("redirecturl", redirecturl);
	}
	
	@RequestMapping("view.htm")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Long id = Long.valueOf(request.getParameter("id"));
		Authorities auth = authoritiesService.getById(id);
		modelMap.put("auth", auth);
		return new ModelAndView(viewView).addAllObjects(modelMap);
	}

	public void setAuthoritiesService(AuthoritiesService authoritiesService) {
		this.authoritiesService = authoritiesService;
	}

}
